# -*- coding: utf-8 -*-
import inspect
from django.core.management.base import NoArgsCommand
from optparse import make_option
from django.conf import settings
from optimaservice.feedback import models, forms


class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list
    help = u'Проверка форм обратной связи'

    def handle_noargs(self, **options):
        for app_name in settings.INSTALLED_APPS:
            try:
                module = __import__(app_name + '.forms', {}, {}, [''])
                for name in dir(module):
                    obj = getattr(module, name)
                    if inspect.isclass(obj) and hasattr(obj, 'slug'): 
                        if hasattr(obj, 'feedback_form') and obj.feedback_form:
                            slug = obj.slug
                            record, created = models.FeedbackFormManager.objects.get_or_create(slug=slug)
                            if created:
                                record.title = obj.title
                                record.save()                        
            except ImportError:
                pass
